﻿<UserControl x:Class="GTServices.WPF.Client.Views.GrepResultsTreeView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
        xmlns:local="clr-namespace:GTServices.WPF.Client.ViewModels"
        xmlns:ext="clr-namespace:GTServices.WPF.Client.Extensions"
        xmlns:entities="clr-namespace:GTServices.Entities;assembly=GTServices.Entities"
           
             >
    <Grid x:Name="_rootElement">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="40*" />
            <ColumnDefinition Width="8" />
            <ColumnDefinition Width="60*" />
        </Grid.ColumnDefinitions>
        
        <DockPanel Grid.Column="0">
            <DockPanel.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Import Results Xml" cal:Message.Attach="ImportResults">
                        
                    </MenuItem>
                </ContextMenu>
            </DockPanel.ContextMenu>
            <!-- cal:Message.Attach="[Event SelectedItemChanged] = [Action SelectedItemChanged($executionContext)]"-->
            <TreeView x:Name="_treeView" ItemsSource="{Binding RootGrepResults}">
                <TreeView.ItemContainerStyle>
                    <Style TargetType="TreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}">
                        <Style.Resources>
                            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
                            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
                            <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
                        </Style.Resources>
                        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                        <Setter Property="FontWeight" Value="Normal" />
                    </Style> <!-- A whole bunch of stuff you dont wantto read ;)-->
                </TreeView.ItemContainerStyle>
                <TreeView.Resources>
                    <ResourceDictionary>
                        <HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type local:GrepResultTreeViewItemViewModel}">
                            <!-- Need to display the Path -->
                            <DockPanel Tag="{Binding Path=DataContext, ElementName=_rootElement}">
                                <DockPanel.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Header="Remove"
                                                        DataContext="{Binding Path=Parent.PlacementTarget.Tag, RelativeSource={RelativeSource Mode=Self}}"  
                                                        cal:Message.Attach="[Event Click] = [Action RemoveGrepResult($executionContext)]"
                                                        cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                                                        Tag="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                                                        />
                                        <MenuItem Header="Export As Xml File"
                                                        DataContext="{Binding Path=Parent.PlacementTarget.Tag, RelativeSource={RelativeSource Mode=Self}}"  
                                                        cal:Message.Attach="[Event Click] = [Action ExportResult($executionContext)]"
                                                        cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                                                        Tag="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                                                        />
                                        <MenuItem Header="Set RootPath"
                                                        DataContext="{Binding Path=Parent.PlacementTarget.Tag, RelativeSource={RelativeSource Mode=Self}}"  
                                                        cal:Message.Attach="[Event Click] = [Action SetRootPath($executionContext)]"
                                                        cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                                                        Tag="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                                                        />
                                    </ContextMenu>
                                </DockPanel.ContextMenu>
                                <TextBlock Text="{Binding TotalMatchInfos, StringFormat=({0})}"
                                           DockPanel.Dock="Right"/>
                                <TextBlock Text="{Binding GrepResult.GrepRequest.RootPath}" 
                                           Tag="{Binding DataContext, ElementName=_rootElement}" 
                                           DockPanel.Dock="Left"
                                           />
                            </DockPanel>
                        </HierarchicalDataTemplate>
                        <DataTemplate DataType="{x:Type local:CategoryTreeViewItemViewModel}">
                            <!-- Need to display the Category Name -->
                            <Expander Tag="{Binding DataContext, ElementName=_rootElement}">
                                <Expander.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Header="Remove"
                                                        DataContext="{Binding Path=Parent.PlacementTarget.Tag, RelativeSource={RelativeSource Mode=Self}}"  
                                                        cal:Message.Attach="[Event Click] = [Action RemoveCategoryFromGrepResult($executionContext)]"
                                                        cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                                                        Tag="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                                                        />
                                    </ContextMenu>
                                </Expander.ContextMenu>
                                <Expander.Header>
                                    <DockPanel>
                                        <TextBlock HorizontalAlignment="Right" 
                                               Text="{Binding Children.Count, StringFormat={} - {0}}"
                                               DockPanel.Dock="Right"/>

                                        <TextBlock Text="{Binding Category.Name}" 
                                               Tag="{Binding DataContext, ElementName=_rootElement}" 
                                               DockPanel.Dock="Left" />
                                    </DockPanel>
                                </Expander.Header>
                                <!--[Event MouseDoubleClick] = [Action ShowResultInWin($executionContext)] -->
                                <ListView           ItemsSource="{Binding Children}"
                                                    Height="300" 
                                                    BorderThickness="0"
                                                    SelectionMode="Extended"
                                                    ext:GridViewSort.AutoSort="True"
                                                    cal:Message.Attach="[Event SelectionChanged] = [Action SelectedItemChanged($executionContext)]"
                                                    cal:Action.TargetWithoutContext="{Binding Path=Tag, RelativeSource={RelativeSource AncestorType={x:Type Expander}}}"
                                                    SelectedItem="{Binding Path=DataContext.SelectedMatchInfo, ElementName=_rootElement}">
                                   
                                    <ListView.Resources>
                                        <Style TargetType="{x:Type ListViewItem}">
                                            <Setter Property="cal:Message.Attach" Value="[Event MouseDoubleClick] = [Action ShowResultInWin($executionContext)]" />
                                        </Style>
                                    </ListView.Resources>
                                    <ListView.ContextMenu>
                                        <ContextMenu>
                                            <MenuItem Header="Remove"
                                                              DataContext="{Binding Path=Parent.PlacementTarget.Parent.Tag, RelativeSource={RelativeSource Mode=Self}}"  
                                                              cal:Message.Attach="[Event Click] = [Action RemoveMatchInfoAction($executionContext)]" 
                                                              Tag="{Binding Path=Parent.PlacementTarget.DataContext, RelativeSource={RelativeSource Mode=Self}}"
                                                                />
                                            <MenuItem Header="Mark Reviewed"
                                                             DataContext="{Binding Path=Parent.PlacementTarget.Parent.Tag, RelativeSource={RelativeSource Mode=Self}}"   
                                                              cal:Message.Attach="[Event Click] = [Action MarkReviewed($executionContext)]"    />
                                            <MenuItem Header="Copy FilePath"
                                                             DataContext="{Binding Path=Parent.PlacementTarget.Parent.Tag, RelativeSource={RelativeSource Mode=Self}}"   
                                                              cal:Message.Attach="[Event Click] = [Action CopySelectedItemsPath($executionContext)]"    />

                                            <MenuItem Header="ToBug"
                                                              DataContext="{Binding Path=Parent.PlacementTarget.Parent.Tag, RelativeSource={RelativeSource Mode=Self}}"  
                                                              cal:Message.Attach="[Event Click] = [Action ToBug($executionContext)]" />
                                            <MenuItem Header="Configure">
                                                <MenuItem Header="Hide/Show Reviewed" IsCheckable="True"
                                                          
                                                          IsChecked="{Binding HideReviewed}"
                                                          DataContext="{Binding Path=Parent.Parent.PlacementTarget.Parent.Tag, RelativeSource={RelativeSource Mode=Self}}"  
                                                          />
                                            </MenuItem>
                                        </ContextMenu>
                                    </ListView.ContextMenu>
                                    <ListView.View>
                                        <GridView>
                                            <GridViewColumn Width="30" ext:GridViewSort.PropertyName="Reviewed">
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <CheckBox IsChecked="{Binding Reviewed}"
                                                                  cal:Message.Attach="[Clicked]=[ReviewedChanging($executionContext)]"/>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>
                                            <GridViewColumn Width="80"  Header="File"  ext:GridViewSort.PropertyName="MatchInfo.FileInfo.Name">
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock Text="{Binding MatchInfo.FileInfo.Name}" ToolTip="{Binding MatchInfo.FileInfo.FullName}" />
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>
                                            <GridViewColumn Width="40"  Header="Line#" DisplayMemberBinding="{Binding MatchInfo.LineNumber}" ext:GridViewSort.PropertyName="MatchInfo.LineNumber"  />
                                            <GridViewColumn Width="80"  Header="Pattern"   DisplayMemberBinding="{Binding MatchInfo.Pattern.PatternStr}" ext:GridViewSort.PropertyName="MatchInfo.Pattern.PatternStr" />
                                            <GridViewColumn Width="500" Header="Context" ext:GridViewSort.PropertyName="MatchInfo.Context">
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock Text="{Binding MatchInfo.Line}" ToolTip="{Binding MatchInfo.Context}" />
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>
                                        </GridView>
                                    </ListView.View>
                                </ListView>
                            </Expander>
                        </DataTemplate>
                    </ResourceDictionary>
                </TreeView.Resources>
            </TreeView>       
        </DockPanel>
        <GridSplitter HorizontalAlignment="Stretch" Width="8" Grid.Column="1" />
        <ContentControl Grid.Column="2" cal:View.Model="{Binding MatchInfoEditorViewModel}" />
    </Grid>
</UserControl>
